package com.yaozheng.vocationaltraining.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.bumptech.glide.Glide;
import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.activity.BaseActivity;
import com.yaozheng.vocationaltraining.utils.BitmapLoader;
import com.yaozheng.vocationaltraining.utils.TypeUtils;
import com.yaozheng.vocationaltraining.view.collect.MyCollectItemView;
import com.yaozheng.vocationaltraining.view.collect.MyCollectItemView_;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 我的收藏设备器
 */
public class MyCollectAdapter extends BaseAdapter {
    JSONArray dataList;
    BitmapLoader bitmapLoader;
    private BaseActivity baseActivity;

    public MyCollectAdapter(BaseActivity baseActivity, JSONArray dataList) {
        super();
        this.baseActivity = baseActivity;
        this.dataList = dataList;
        bitmapLoader = new BitmapLoader(Glide.with(baseActivity),
                R.drawable.default_load_image);
    }


    public JSONArray getDataList() {
        return dataList;
    }

    public void setDataList(JSONArray dataList) {
        this.dataList = dataList;
    }

    /**
     * 得到最后id
     *
     * @return
     */
    public int getLastId() {
        if (dataList != null && TypeUtils.getJsonArraySize(dataList) > 0) {

            JSONObject itemData = TypeUtils.getJsonObject(dataList,
                    TypeUtils.getJsonArraySize(dataList) - 1);
            if (itemData != null) {
                return TypeUtils.getJsonInteger(itemData, "id", -1);
            }
        }
        return -1;
    }

    @Override
    public int getCount() {
        return TypeUtils.getJsonArraySize(dataList);
    }

    @Override
    public JSONObject getItem(int position) {
        return TypeUtils.getJsonObject(dataList, position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyCollectItemView myCollectItemView;
        if (convertView == null) {
            myCollectItemView = MyCollectItemView_.build(baseActivity);
        } else {
            myCollectItemView = (MyCollectItemView) convertView;
        }
        myCollectItemView.loadData(position, TypeUtils.getJsonObject(dataList, position), bitmapLoader);
        return myCollectItemView;
    }
}
